package com.betterjr.modules.customer.service;

import java.util.List;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.betterjr.common.service.BaseService;
import com.betterjr.common.utils.BTAssert;
import com.betterjr.modules.account.entity.CustOperatorInfo;
import com.betterjr.modules.account.entity.CustOperatorInfoRequest;
import com.betterjr.modules.customer.dao.CustCloudPlatformOperatorMapper;
import com.betterjr.modules.customer.entity.CustCloudPlatformAll;
import com.betterjr.modules.customer.entity.CustCloudPlatformCustom;
import com.betterjr.modules.customer.entity.CustCloudPlatformOperator;
import com.betterjr.modules.customer.entity.CustCloudPlatformTenant;
import com.betterjr.modules.operator.service.OperatorRequestService;

/**
 * 
 * @ClassName: CustCloudPlatformUserService 
 * @Description: 云平台数据服务 
 * @author xuzx
 * @date 2018年1月19日 下午2:49:18 
 *
 */
@Service
public class CustCloudPlatformUserService
        extends BaseService<CustCloudPlatformOperatorMapper, CustCloudPlatformOperator> {

    @Autowired
    private CustCloudPlatformTenantService custCloudPlatformTenantService;

    @Autowired
    private CustCloudPlatformCustomService custCloudPlatformCustomService;

    @Autowired
    private OperatorRequestService operatorRequestService;

    /**
     * 
     * @Title: saveCloudPlatformDate 
     * @Description: 同步云平台登录的数据 
     * @param cloudDate 云平台登录的数据
     * @return 
     * @throws 
     * @author xuzx
     * @date 2018年1月19日 下午2:49:50
     */
    public CustCloudPlatformAll saveCloudPlatformDate(String cloudDate) {
        CustCloudPlatformAll custCloudPlatformAll = JSONObject.parseObject(cloudDate, CustCloudPlatformAll.class);
        String operorg = null;
        // 租户信息
        CustCloudPlatformTenant cust = custCloudPlatformAll.getCust();
        CustCloudPlatformTenant tenant = custCloudPlatformTenantService.getTenantInformation(cust.getCustID());
        if (tenant == null) {
            operorg = cust.getCorpName() + getStringRandom(10);
        } else {
            operorg = tenant.getOperorg();
        }
        custCloudPlatformTenantService.saveTenantInformation(cust, operorg);

        // 客户信息
        List<CustCloudPlatformCustom> enteList = custCloudPlatformAll.getEnte();
        custCloudPlatformCustomService.saveCloudPlatformCustomer(enteList, operorg);

        // 操作员信息
        CustCloudPlatformOperator user = custCloudPlatformAll.getUser();
        saveCloudPlatformOperator(user, operorg);
        return custCloudPlatformAll;

    }

    /**
     * 
     * @Title: saveCloudPlatformOperator 
     * @Description: 同步云平台操作员数据 
     * @param anUser  操作员信息
     * @param anOperorg 操作员组织机构
     * @return 同步数量
     * @throws 
     * @author xuzx
     * @date 2018年1月19日 下午2:50:26
     */
    public void saveCloudPlatformOperator(final CustCloudPlatformOperator anUser, final String anOperorg) {
        CustCloudPlatformOperator user = anUser;
        Long userId = anUser.getUserId();
        CustCloudPlatformOperator cloudPlatformOperator = this.selectByPrimaryKey(userId);
        if (cloudPlatformOperator != null) {
            // 企e金服用户表相关维护
            CustOperatorInfo custOperatorInfo = operatorRequestService
                    .findCloudPlatformOperatorById(cloudPlatformOperator.getAssociatedID());
            custOperatorInfo.setOperCode(user.getUserCode());
            custOperatorInfo.setName(user.getUserName());
            operatorRequestService.updateCloudPlatformOperator(custOperatorInfo);
            // 云平台用户暂存表维护
            cloudPlatformOperator.setCustID(user.getCustID());
            cloudPlatformOperator.setUserName(user.getUserName());
            cloudPlatformOperator.setEnteID(user.getEnteID());
            cloudPlatformOperator.setUserCode(user.getUserCode());
            this.updateByPrimaryKey(cloudPlatformOperator);
        } else {
            // 企e金服用户表相关维护
            CustOperatorInfoRequest infoRequest = new CustOperatorInfoRequest();
            infoRequest.setName(user.getUserName());
            infoRequest.setOperOrg(anOperorg);
            infoRequest.setOperCode(user.getUserCode());
            infoRequest.setPassword("123456");
            Long associatedId = operatorRequestService.saveCloudPlatformOperator(infoRequest);
            // 云平台用户暂存表维护
            user.setAssociatedID(associatedId);
            user.initAddValue(user);
            user.setOperorg(anOperorg);
            this.insert(user);
        }
    }

    /**
     * 
     * @Title: queryOperatorByUserId 
     * @Description: 根据操作员id，在云平台操作员暂存表查找信息 
     * @param anUserId  操作员id
     * @return 操作员信息
     * @throws 
     * @author xuzx
     * @date 2018年1月19日 下午2:51:24
     */
    public CustCloudPlatformOperator queryOperatorByUserId(String anUserId) {
        BTAssert.notNull(anUserId, "传入操作员id为空！");
        Long userId = Long.valueOf(anUserId);
        CustCloudPlatformOperator operator = this.selectByPrimaryKey(userId);
        return operator;

    }

    /**
     * 
     * @Title: queryCertInfoSerialNo 
     * @Description: 根据云平台同步数据查询证书序列号 
     * @param cloudDate 云平台同步的数据
     * @return 集团信息的证书对应证书序列号
     * @throws 
     * @author xuzx
     * @date 2018年1月19日 下午2:53:41
     */
    public String queryCertInfoSerialNo(String cloudDate) {
        CustCloudPlatformAll custCloudPlatformAll = JSONObject.parseObject(cloudDate, CustCloudPlatformAll.class);
        CustCloudPlatformTenant cust = custCloudPlatformAll.getCust();
        return custCloudPlatformTenantService.getTenantInformation(cust.getCustID()).getSerialNo();
    }

    /**
     * 
     * @Title: getStringRandom 
     * @Description: 随机生成10位数字+字母字符串 
     * @param length  字符串长度
     * @return 随机生成的字符串
     * @throws 
     * @author xuzx
     * @date 2018年1月19日 下午2:52:10
     */
    public String getStringRandom(int length) {
        String val = "";
        Random random = new Random();
        // 参数length，表示生成几位随机数
        for (int i = 0; i < length; i++) {
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            // 输出字母还是数字
            if ("char".equalsIgnoreCase(charOrNum)) {
                // 输出是大写字母还是小写字母
                int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
                val += (char) (random.nextInt(26) + temp);
            } else if ("num".equalsIgnoreCase(charOrNum)) {
                val += String.valueOf(random.nextInt(10));
            }
        }
        return val;
    }
}
